package mrpheu.demo.client.tcp;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatTCPClient {

    private Socket socket = null;
    private BufferedReader streamIn = null;
    private DataOutputStream streamOut = null;
    private ChatTCPClientThread clientThread = null;

    private String serverName = "";
    private int port = 12345; // default
    
    private boolean off = false; // default

    public ChatTCPClient(String serverName, int port) {
        this.serverName = serverName;
        this.port = port;
    }

    public void open() {
        System.out.println("Connecting to server...");
        try {
            this.socket = new Socket(this.serverName, this.port);
            this.streamIn = new BufferedReader(new InputStreamReader(System.in));
            this.streamOut = new DataOutputStream(this.socket.getOutputStream());
        } catch (UnknownHostException e) {
            System.out.println("Connect to server not found");
            this.off = true;
            return;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Connect to server to fail");
            this.off = true;
            return;
        }
        System.out.println("Connected to server " + this.socket);
    }
    
    public void process() {
        System.out.println("Begin transfer:");
        String line = "";
        while(!this.off) {
            try {
                System.out.print("Input message: ");
                line = this.streamIn.readLine();
                System.out.println("You:" + line);
                String[] lineArr = line.split("@");
                System.out.println("Length:" + lineArr.length);
                if(lineArr.length < 2) {
                    if(this.clientThread.getId() != null) {
                        line = this.clientThread.getId() + ":" + line;
                    }
                }
                this.streamOut.writeUTF(line);
                this.streamOut.flush();
                this.off = line.equals(".bye");
            } catch (IOException e) {
                System.out.println("I/O transfer fail.");
                this.off = true;
            }
        }
        System.out.println("End transfer.");
        this.close();
    }

    public void close() {
        try {
            if (this.streamIn != null) {
                this.streamIn.close();
            }
            if(this.streamOut != null) {
                this.streamOut.close();
            }
            if(this.socket != null) {
                this.socket.close();
            }
        } catch (IOException e) {
            System.out.println("Close server fail");
        }
    }
    
    public void start() {
        this.open();
        this.clientThread = new ChatTCPClientThread(this.socket);
        new Thread(this.clientThread).start();
        this.process();
    }

    public static void main(String[] args) {
        ChatTCPClient client = new ChatTCPClient("mrpheu-daviteq", 54321);
        client.start();
    }

}
